﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DiBang.Common;
using DiBang.Sql;
using System.IO;
using System.IO.Ports;
using System.Threading;

namespace ChangWuDiBang
{
    public partial class FrmDiBangFirstInput : Office2007Form
    {
        public SerialPort _SerialPort
        {
            get;
            set;
        }

        public UserInfo _UserInfo
        {
            get;
            set;
        }

        SqlLib sqllib = new SqlLib();
        SqlExtend sqlextend = new SqlExtend();

        private delegate void UpdateTextEventHandler(string text);  //委托，此为重点
        private UpdateTextEventHandler updateText;
        private void UpdateTextBox(string text)
        {
            txtPiZhong.Text = text;
        }

        private bool closeFlag = false;

        public FrmDiBangFirstInput()
        {
            this.EnableGlass = false;
            InitializeComponent();
        }

        private void FrmDiBangFirstInput_Load(object sender, EventArgs e)
        {
            if (_UserInfo == null|| string.IsNullOrEmpty(_UserInfo.UserID))
            {
                return;
            }

            //
            txtUserID.Text = _UserInfo.UserID;

            this._SerialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.port_DataReceived);
            updateText = new UpdateTextEventHandler(UpdateTextBox);//实例化委托对象
            //
            SetInfo();

            //this.timer1.Enabled = true;
            //this.timer1.Start();
        }

        private void port_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            if (!closeFlag)
            {
                Thread.Sleep(10);
                byte[] readBuffer = new byte[_SerialPort.BytesToRead];
                _SerialPort.Read(readBuffer, 0, readBuffer.Length);

                double dResult = SerialPortProcess.GetBuffer(readBuffer);

                //
                if (dResult > -1)
                {
                    this.Invoke(updateText, new string[] { dResult.ToString() });
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtPinMing.Text.Trim()))
            {
                MessageBoxEx.Show("品名不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtPinMing.Focus();
                return;
            }

            if (string.IsNullOrEmpty(txtCheHao.Text.Trim()))
            {
                MessageBoxEx.Show("车号不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtCheHao.Focus();
                return;
            }

            UserInfo tempUserInfo = sqlextend.GetUserInfo(txtUserID.Text);
            if (tempUserInfo.UserGroup != ConstParam.UserGroup_Administrator
               && tempUserInfo.UserGroup != ConstParam.UserGroup_User)
            {
                MessageBoxEx.Show("没有修改的权限,请确认!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (!sqlextend.UserPwdCheck(txtUserID.Text, txtPwd.Text))
            {
                MessageBoxEx.Show("账户或密码不正确!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            string lasteventtimekey = sqlextend.GetTimeKey();
            Dictionary<string, object> setMap = new Dictionary<string, object>();
            setMap.Clear();
            setMap.Add("CHE_HAO", txtCheHao.Text.Trim());
            setMap.Add("PIN_MING", txtPinMing.Text.Trim());
            setMap.Add("OTHER", txtOther.Text.Trim());

            setMap.Add("PIZHONG", Base64Encoding.BaseEncoding(txtPiZhong.Text.Trim()));//需要加密存储

            setMap.Add("CREATE_USER_ID", txtUserID.Text.Trim());
            setMap.Add("CREATE_TIME", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));

            setMap.Add("LAST_EVENT_NAME", "新增");
            setMap.Add("LAST_EVENT_USER_ID", txtUserID.Text);
            setMap.Add("LAST_EVENT_TIMEKEY", lasteventtimekey);

            if (sqllib.InsertTable("DB_DIBANG", setMap) > -1)
            {
                sqlextend.MakeDiBangHistory(lasteventtimekey);//插入履历
                MessageBoxEx.Show("新增成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            SetInfo();
        }

        private void SetInfo()
        {
            string strTime = sqlextend.GetSysTime();
            //string diBangRead = ConstParam.DiBangRead;

            lblVDate.Text = strTime;
            //txtMaoZhong.Text = diBangRead;
        }

        private void FrmDiBangFirstInput_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.closeFlag = true;
            this._SerialPort.DataReceived -= new System.IO.Ports.SerialDataReceivedEventHandler(this.port_DataReceived);
            Thread.Sleep(200);
        }
    }
}
